/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * altaPrueba.java
 *
 * Created on 02-jun-2011, 17:12:32
 */

package interfaces;

import Controlador.*;
import GestorCitas.Cita;
import GestorPruebas.*;
import GestorUsuarios.IDException;
import clinica.ClinicaView;
import clinica.Interfaces;
import clinica.Interfaz;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jdesktop.application.Action;

/**
 *
 * @author antonio
 */
public class altaPrueba extends javax.swing.JPanel {

    private ClinicaView app;
    private Prueba _prueba = null ;
    private Cita _cita = null;
    private UsuariosDisponibles _disponibles[] = null;
    Boolean _modificacion ;
    Boolean _inicio = true;
    
    /** Creates new form altaPrueba */
    public altaPrueba(ClinicaView a) {
        initComponents();
        app = a;
        
        fecha.setDate(Calendar.getInstance().getTime());
        _inicio = true; //para evitar que calcule en el constructor
    }

    public void rellenaDatos ( Cita c , Prueba p , Interfaces ant ) {
        _inicio = false;
        _cita = c;
        tiposRadiografia.setVisible(false);
        jLabel_tipoRadiografia.setVisible(false);
        if ( p != null ) {
            _modificacion = true;
            Calendar cal = Calendar.getInstance();
            cal.setTime(p.getFecha());
            horas.setSelectedIndex(cal.get(Calendar.HOUR_OF_DAY)-8);
            fecha.setDate(p.getFecha());
            

            switch (cal.get(Calendar.MINUTE)) {
                case 0:
                    jRadioButton1.setSelected(true); break;
                case 20:
                    jRadioButton2.setSelected(true); break;
                case 40:
                    jRadioButton3.setSelected(true); break;
                default:
                    break;
            }

            _prueba = p;

            //Seleccionamos la prueba elegida
            switch (_prueba.getTipo()) {
            case BioquimicaGeneral:
                tipoPrueba.setSelectedIndex(0);break;
            case HematologiaGeneral:
                tipoPrueba.setSelectedIndex(1);break;
            case MarcadoresTumorales:
                tipoPrueba.setSelectedIndex(2);break;
            case Hormonas:
                tipoPrueba.setSelectedIndex(3);break;
            case Urianalisis:
                tipoPrueba.setSelectedIndex(4);break;
            case Hemostasia:
                tipoPrueba.setSelectedIndex(5);break;
            case Radiografia:
                tipoPrueba.setSelectedIndex(6);
                tiposRadiografia.setVisible(true);
                jLabel_tipoRadiografia.setVisible(true);
                switch (((Radiografia)_prueba).getTipoRadiografia()) {
                    case CR:
                        tiposRadiografia.setSelectedIndex(0);break;
                    case SD:
                        tiposRadiografia.setSelectedIndex(1);break;
                    case SI:
                        tiposRadiografia.setSelectedIndex(2);break;
                    case ID:
                        tiposRadiografia.setSelectedIndex(3);break;
                    case II:
                        tiposRadiografia.setSelectedIndex(4);break;
                    case CL:
                        tiposRadiografia.setSelectedIndex(5);break;
                    case H:
                        tiposRadiografia.setSelectedIndex(6);break;
                    case T:
                        tiposRadiografia.setSelectedIndex(7);break;
                    case A:
                        tiposRadiografia.setSelectedIndex(8);break;
                    case TC:
                        tiposRadiografia.setSelectedIndex(9);break;
                    case TM:
                        tiposRadiografia.setSelectedIndex(10);break;
                    default:
                        tiposRadiografia.setSelectedIndex(0);break;
                }
                break;
            default:
                tipoPrueba.setSelectedIndex(0);break;
          }//end switch


        }else {
            limpiaDatos();
            _modificacion = false;
        }

        

    }

    public void limpiaDatos ( ) {
        fecha.setDate(Calendar.getInstance().getTime());
        horas.setSelectedIndex(0);
        tipoPrueba.setSelectedIndex(0);
        buttonGroup1.clearSelection();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        horas = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jRadioButton3 = new javax.swing.JRadioButton();
        fecha = new com.toedter.calendar.JDateChooser();
        tipoPrueba = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        tiposRadiografia = new javax.swing.JComboBox();
        jLabel_tipoRadiografia = new javax.swing.JLabel();
        aceptar = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setMinimumSize(new java.awt.Dimension(780, 472));
        setPreferredSize(new java.awt.Dimension(780, 472));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Datos para concertar la prueba", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, Interfaz.setFontSize()));
        jPanel1.setName("jPanel1"); // NOI18N

        horas.setFont(Interfaz.setFontSize());
        horas.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "08:00", "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00", "18:00", "19:00", "20:00" }));
        horas.setName("horas"); // NOI18N
        horas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                horasActionPerformed(evt);
            }
        });

        jLabel1.setFont(Interfaz.setFontSize());
        jLabel1.setText("Día a realizar la prueba:");
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setFont(Interfaz.setFontSize());
        jLabel2.setText("Hora:");
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setFont(Interfaz.setFontSize());
        jLabel3.setText("Franja minutos:");
        jLabel3.setName("jLabel3"); // NOI18N

        jPanel2.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPanel2.setName("jPanel2"); // NOI18N

        buttonGroup1.add(jRadioButton1);
        jRadioButton1.setFont(Interfaz.setFontSize());
        jRadioButton1.setText("hh:00 a hh:20");
        jRadioButton1.setName("jRadioButton1"); // NOI18N

        buttonGroup1.add(jRadioButton2);
        jRadioButton2.setFont(Interfaz.setFontSize());
        jRadioButton2.setText("hh:20 a hh:40");
        jRadioButton2.setName("jRadioButton2"); // NOI18N

        buttonGroup1.add(jRadioButton3);
        jRadioButton3.setFont(Interfaz.setFontSize());
        jRadioButton3.setText("hh:40 a hh:60");
        jRadioButton3.setName("jRadioButton3"); // NOI18N

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jRadioButton1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE)
                    .add(jRadioButton3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE)
                    .add(jRadioButton2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 281, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jRadioButton1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jRadioButton2)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jRadioButton3)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        fecha.setDate(Calendar.getInstance().getTime());
        fecha.setFont(new java.awt.Font("Tahoma", 0, 20)); // NOI18N
        fecha.setName("fecha"); // NOI18N
        fecha.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                fechaPropertyChange(evt);
            }
        });

        tipoPrueba.setFont(Interfaz.setFontSize());
        tipoPrueba.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Bioquímica General", "Hematologia General", "Marcadores Tumorales", "Hormonas", "Ánalisis de orina", "Hemostasia", "Radiografía" }));
        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance().getContext().getActionMap(altaPrueba.class, this);
        tipoPrueba.setAction(actionMap.get("tiposRadiografia")); // NOI18N
        tipoPrueba.setName("tipoPrueba"); // NOI18N

        jLabel4.setFont(Interfaz.setFontSize());
        jLabel4.setText("Seleccione el tipo de prueba:");
        jLabel4.setName("jLabel4"); // NOI18N

        tiposRadiografia.setFont(Interfaz.setFontSize());
        tiposRadiografia.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Cráneo", "Extremidad Superior Drcha", "Extremidad Superior Izq.", "Extremidad Inferior Drcha.", "Extremidad Inferior Izq.", "Columna", "Hombro", "Torax", "Abdomen", "Telemática Columna", "Telemática MII" }));
        tiposRadiografia.setName("tiposRadiografia"); // NOI18N

        jLabel_tipoRadiografia.setFont(Interfaz.setFontSize());
        jLabel_tipoRadiografia.setText("Tipo prueba radiológica:");
        jLabel_tipoRadiografia.setName("jLabel_tipoRadiografia"); // NOI18N

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(jLabel_tipoRadiografia)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, fecha, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 352, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, tiposRadiografia, 0, 352, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel1)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel4)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, tipoPrueba, 0, 352, Short.MAX_VALUE))
                        .add(18, 18, 18)))
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel2)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel1Layout.createSequentialGroup()
                        .add(21, 21, 21)
                        .add(horas, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 170, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jLabel3))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(jLabel2))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(horas, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(fecha, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 57, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(23, 23, 23)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel4)
                    .add(jLabel3))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(tipoPrueba, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(18, 18, 18)
                        .add(jLabel_tipoRadiografia)
                        .add(18, 18, 18)
                        .add(tiposRadiografia, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(61, 61, 61))
        );

        aceptar.setAction(actionMap.get("Aceptar")); // NOI18N
        aceptar.setFont(new java.awt.Font("Tahoma 18 18", 0, 12));
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(altaPrueba.class);
        aceptar.setIcon(resourceMap.getIcon("aceptar.icon")); // NOI18N
        aceptar.setName("aceptar"); // NOI18N

        jButton1.setAction(actionMap.get("cancelar")); // NOI18N
        jButton1.setIcon(resourceMap.getIcon("jButton1.icon")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(jButton1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(aceptar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 362, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(aceptar)
                    .add(jButton1))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void horasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_horasActionPerformed
        habilitarFranja();
        
}//GEN-LAST:event_horasActionPerformed

    private void fechaPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_fechaPropertyChange

        Calendar calIni = Calendar.getInstance() , calFin = Calendar.getInstance();
        //Hora inicio
        calIni.setTime(fecha.getDate());
        calIni.set(Calendar.HOUR_OF_DAY, horas.getSelectedIndex()+8);
        calIni.set(Calendar.SECOND, 0);
        calIni.set(Calendar.MINUTE,0);

        //Hora fin
        calFin.setTime(fecha.getDate());
        calFin.set(Calendar.HOUR_OF_DAY, 21);
        calFin.set(Calendar.SECOND, 0);
        calFin.set(Calendar.MINUTE,0);

        try {
            if (!_inicio) this._disponibles = Controlador.concertarPrueba(calIni.getTime(), calFin.getTime());
        } catch (IOException ex) {
            Logger.getLogger(altaPrueba.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(altaPrueba.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IDException ex) {
            Logger.getLogger(altaPrueba.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(altaPrueba.class.getName()).log(Level.SEVERE, null, ex);
        }

        //Habilitamos los botones
        habilitarFranja();
    }//GEN-LAST:event_fechaPropertyChange


    void habilitarFranja () {
        buttonGroup1.clearSelection();
        jRadioButton1.setEnabled(false);
        jRadioButton2.setEnabled(false);
        jRadioButton3.setEnabled(false);
        if (_disponibles != null)
        {
            Calendar c = Calendar.getInstance();
            for ( UsuariosDisponibles u: _disponibles ) {
                c.setTime(u.getFechaInicio());
                if ( c.get(Calendar.HOUR_OF_DAY) == horas.getSelectedIndex()+8 && u.getArrayId().size() > 0 ){
                    switch (c.get(Calendar.MINUTE)) {
                        case 0:
                            jRadioButton1.setEnabled(true); break;
                        case 20:
                            jRadioButton2.setEnabled(true); break;
                        case 40:
                            jRadioButton3.setEnabled(true); break;
                        default:
                            break;
                    }
                }
            }
        }
    }

    @Action
    public void Aceptar() throws IOException, ClassNotFoundException, SQLException {
        app.limpiaErrores();
        Boolean error = false;
        Date today = Calendar.getInstance().getTime();
        Calendar c = Calendar.getInstance();

        //Vemos la fecha de prueba seleccionada
        try {
            c.setTime(fecha.getDate());
        } catch (Exception p ) {
            app.addError("Formato de fecha incorrecto");
            error = true;
        }
        
        //Tipo de prueba
        switch (tipoPrueba.getSelectedIndex()) {
            case 0:
                _prueba = new BioquimicaGeneral();
                _prueba.setTipo(TipoPrueba.BioquimicaGeneral);
                break;
            case 1:
                _prueba = new HematologiaGeneral();
                _prueba.setTipo(TipoPrueba.HematologiaGeneral);
                break;
            case 2:
                _prueba = new MarcadoresTumorales();
                _prueba.setTipo(TipoPrueba.MarcadoresTumorales);
                break;
            case 3:
                _prueba = new Hormonas();
                _prueba.setTipo(TipoPrueba.Hormonas);
                break;
            case 4:
                _prueba = new Urianalisis();
                _prueba.setTipo(TipoPrueba.Urianalisis);
                break;
            case 5:
                _prueba = new Hemostasia();
                _prueba.setTipo(TipoPrueba.Hemostasia);
                break;
            case 6:
                _prueba = new Radiografia();
                _prueba.setTipo(TipoPrueba.Radiografia);
                switch (tiposRadiografia.getSelectedIndex()) {
                    case 0:
                        ((Radiografia)_prueba).setTipoRadiografia(TipoRadiografia.CR); break;
                    case 1:
                        ((Radiografia)_prueba).setTipoRadiografia(TipoRadiografia.SD); break;
                    case 2:
                        ((Radiografia)_prueba).setTipoRadiografia(TipoRadiografia.SI); break;
                    case 3:
                        ((Radiografia)_prueba).setTipoRadiografia(TipoRadiografia.ID); break;
                    case 4:
                        ((Radiografia)_prueba).setTipoRadiografia(TipoRadiografia.II); break;
                    case 5:
                        ((Radiografia)_prueba).setTipoRadiografia(TipoRadiografia.CL); break;
                    case 6:
                        ((Radiografia)_prueba).setTipoRadiografia(TipoRadiografia.H); break;
                    case 7:
                        ((Radiografia)_prueba).setTipoRadiografia(TipoRadiografia.T); break;
                    case 8:
                        ((Radiografia)_prueba).setTipoRadiografia(TipoRadiografia.A); break;
                    case 9:
                        ((Radiografia)_prueba).setTipoRadiografia(TipoRadiografia.TC); break;
                    case 10:
                        ((Radiografia)_prueba).setTipoRadiografia(TipoRadiografia.TM); break;
                    default:
                        ((Radiografia)_prueba).setTipoRadiografia(TipoRadiografia.TM); break;
                }
                break;
            default:
                _prueba = new Urianalisis();
                _prueba.setTipo(TipoPrueba.Urianalisis);
                break;
        } //end switch tipoPrueba

        
        //Vemos la hora de cita escogida
        c.set(Calendar.HOUR_OF_DAY, horas.getSelectedIndex()+8);
        if(jRadioButton1.isSelected())
            c.set(Calendar.MINUTE, 0);
        else if(jRadioButton2.isSelected())
            c.set(Calendar.MINUTE, 20);
        else if(jRadioButton3.isSelected())
            c.set(Calendar.MINUTE, 40);
        else {
            app.addError("Debe seleccionar una franja horaria");
            error = true;
        }
        
        //Comprobrobamos que no sea una fecha posterior al dia de hoy
        if (!error)
            if ( c.getTime().before(today)) {
                    app.addError("La fecha no puede ser inferior al dia actual");
                    error = true;
            }else {
                c.set(Calendar.HOUR_OF_DAY, horas.getSelectedIndex()+8);
                c.set(Calendar.SECOND, 0);
                _prueba.setFecha(c.getTime());
            }
        

        //Vemos el id del tecnico (el primero de la lista)
        if (!error)
            for ( UsuariosDisponibles d: _disponibles ) {
                if ( d.getFechaInicio().toString().equals(_prueba.getFecha().toString())) {
                    Random rnd = new Random();
                    int sel = rnd.nextInt(d.getArrayId().size());
                    _prueba.setIdTecnicoPruebas(d.getArrayId().get(sel));
                }
            }
        //Vemos si hemos encontrado un id
        if (_prueba.getIdTecnicoPruebas() == null ) {
            app.addError("Ha habido un problema al buscar un técnico disponible para la prueba, vuelva a intentarlo");
            error = true;
        }
            
        //Estado de la prueba
        _prueba.setEstadoPrueba(EstadoPrueba.Pendiente);

        //Datos de la cita
        _prueba.setIdCita(_cita.getIdCita());
        _prueba.setIdPaciente(_cita.getIdPaciente());
        _prueba.setIdMedico(_cita.getIdMedico());

        //Guardamos la prueba
        if ( !error ) {
            if ( !_modificacion ) {
                //Añadimos a la BD
                try {
                    Prueba.add(_prueba);
                } catch (IDException ex) {
                    Logger.getLogger(altaPrueba.class.getName()).log(Level.SEVERE, null, ex);
                } catch (NoSuchAlgorithmException ex) {
                    Logger.getLogger(altaPrueba.class.getName()).log(Level.SEVERE, null, ex);
                }
                
                app.abrirPantallaCita3(_prueba);
                
                
            }else {
                //Actualizamos de la BD
                try {
                    Prueba.update(_prueba);
                } catch (IDException ex) {
                    Logger.getLogger(altaPrueba.class.getName()).log(Level.SEVERE, null, ex);
                } catch (NoSuchAlgorithmException ex) {
                    Logger.getLogger(altaPrueba.class.getName()).log(Level.SEVERE, null, ex);
                }
                app.abrirPantallaCita3(_prueba);
            }
        }else
            app.muestraListaErrores();

    }

    @Action
    public void cancelar() throws IOException, ClassNotFoundException, SQLException {
        if (_modificacion) {
            try {
                _prueba.setIdPrueba(null);
                Prueba.add(_prueba);
            } catch (IDException ex) {
                Logger.getLogger(altaPrueba.class.getName()).log(Level.SEVERE, null, ex);
            } catch (NoSuchAlgorithmException ex) {
                Logger.getLogger(altaPrueba.class.getName()).log(Level.SEVERE, null, ex);
            }
            app.abrirPantallaCita3(_prueba);
        } else
            app.abrirPantallaCita3(null);
    }

    @Action
    public void tiposRadiografia() {
        if ( tipoPrueba.getSelectedIndex() == 6) {
            tiposRadiografia.setVisible(true);
            jLabel_tipoRadiografia.setVisible(true);
        }else {
            tiposRadiografia.setVisible(false);
            jLabel_tipoRadiografia.setVisible(false);
        }

    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton aceptar;
    private javax.swing.ButtonGroup buttonGroup1;
    private com.toedter.calendar.JDateChooser fecha;
    private javax.swing.JComboBox horas;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel_tipoRadiografia;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JComboBox tipoPrueba;
    private javax.swing.JComboBox tiposRadiografia;
    // End of variables declaration//GEN-END:variables

}
